﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieStorage;
using System.Windows.Forms;

namespace DVDFilmoteka.Sorting
{
    class DateSorter : ISorter
    {
        private readonly List<String> expanded = new List<string>();

        #region ISorter Members

        public void sort(System.Windows.Forms.TreeView treeView, List<MovieDataStore12> movieList)
        {
            Cursor.Current = Cursors.WaitCursor;

            // Smazat
            treeView.Nodes.Clear();

            //List<String> monthNodeList = new List<string>();
            foreach (MovieDataStore12 item in movieList)
            {
                String month = item.timeOfAddtion.Year.ToString() + " - ";
                switch (item.timeOfAddtion.Month)
                {
                    case 1: month += "01. (leden)"; break;
                    case 2: month += "02. (únor)"; break;
                    case 3: month += "03. (březen)"; break;
                    case 4: month += "04. (duben)"; break;
                    case 5: month += "05. (květen)"; break;
                    case 6: month += "06. (červen)"; break;
                    case 7: month += "07. (červenec)"; break;
                    case 8: month += "08. (srpen)"; break;
                    case 9: month += "09. (zaří)"; break;
                    case 10: month += "10. (říjen)"; break;
                    case 11: month += "11. (listopad)"; break;
                    case 12: month += "12. (prosinec)"; break;                    
                }
                
                // Prace s mesicem
                TreeNode selNode = null;                
                foreach (TreeNode item2 in treeView.Nodes)
                {
                    if (item2.Text.Equals(month)) //Najit patricny node s mesicem
                    {
                        selNode = item2;
                        break;
                    }
                }
                if (selNode == null)
                {
                    TreeNode node = new TreeNode(month);
                    node.Tag = new object();
                    treeView.Nodes.Add(node); //Pokud jeste neni tak pridat
                    selNode = node;
                }

                // Prace s dnem v mesici
                String day = item.timeOfAddtion.Day.ToString();
                if (day.Length <= 1) day = "0" + day;

                TreeNode selNode2 = selNode;
                selNode = null;
                foreach (TreeNode item2 in selNode2.Nodes)
                {
                    if (item2.Text.Equals(day)) //Najit patricny node s dnem v mesici
                    {
                        selNode = item2;
                        break;
                    }
                }
                if (selNode == null)
                {
                    TreeNode node = new TreeNode(day);
                    node.Tag = new object();
                    selNode2.Nodes.Add(node); //Pokud jeste neni tak pridat
                    selNode = node;
                }

                // Prejit do konkretniho dne a pridat nazev filmu
                String name = "Není vyplněno jméno!!!";
                if (!string.IsNullOrEmpty(item.czechName))
                {
                    name = item.czechName;
                }
                else if (!string.IsNullOrEmpty(item.originalName))
                {
                    name = item.originalName;
                }               

                TreeNode _node = new TreeNode(name);
                _node.Tag = item; // V treelistu si drzim ukazatel na data o filmu
                selNode.Nodes.Add(_node);
            }

            // Expanduje korenove nody pokud byli predtim expandovane
            if (expanded != null && expanded.Count > 0)
            {
                foreach (TreeNode item in treeView.Nodes)
                {
                    foreach (TreeNode item2 in item.Nodes)
                    {
                        if (expanded.Contains(item2.Text))
                        {
                            item2.Expand();
                        }
                    }

                    if (expanded.Contains(item.Text))
                    {
                        item.Expand();
                    }
                }
            }

            Cursor.Current = Cursors.Arrow;
        }

        public void saveExpanded(System.Windows.Forms.TreeView treeView)
        {
            expanded.Clear();
            foreach (TreeNode item in treeView.Nodes)
            {
                foreach (TreeNode item2 in item.Nodes)
                {
                    if (item2.IsExpanded)
                    {
                        expanded.Add(item2.Text);
                    }
                }

                if (item.IsExpanded)
                {
                    expanded.Add(item.Text);
                }
            }
        }

        public ESorters whoAmI()
        {
            return ESorters.DATETIME;
        }

        #endregion
    }
}
